package com.warehouse.module.business.service.productnametype;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.warehouse.module.business.controller.admin.productnametype.vo.*;
import com.warehouse.module.business.dal.dataobject.productnametype.ProductNameTypeDO;
import com.warehouse.framework.common.pojo.PageResult;
import com.warehouse.framework.common.pojo.PageParam;
import com.warehouse.framework.common.util.object.BeanUtils;

import com.warehouse.module.business.dal.mysql.productnametype.ProductNameTypeMapper;

import static com.warehouse.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.warehouse.module.business.enums.ErrorCodeConstants.*;

/**
 * 品名/型号管理 Service 实现类
 *
 * @author Eric
 */
@Service
@Validated
public class ProductNameTypeServiceImpl implements ProductNameTypeService {

    @Resource
    private ProductNameTypeMapper productNameTypeMapper;

    @Override
    public Long createProductNameType(ProductNameTypeSaveReqVO createReqVO) {
        // 插入
        ProductNameTypeDO productNameType = BeanUtils.toBean(createReqVO, ProductNameTypeDO.class);
        productNameTypeMapper.insert(productNameType);
        // 返回
        return productNameType.getId();
    }

    @Override
    public void updateProductNameType(ProductNameTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateProductNameTypeExists(updateReqVO.getId());
        // 更新
        ProductNameTypeDO updateObj = BeanUtils.toBean(updateReqVO, ProductNameTypeDO.class);
        productNameTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductNameType(Long id) {
        // 校验存在
        validateProductNameTypeExists(id);
        // 删除
        productNameTypeMapper.deleteById(id);
    }

    private void validateProductNameTypeExists(Long id) {
        if (productNameTypeMapper.selectById(id) == null) {
            throw exception(PRODUCT_NAME_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ProductNameTypeDO getProductNameType(Long id) {
        return productNameTypeMapper.selectById(id);
    }

    @Override
    public PageResult<ProductNameTypeDO> getProductNameTypePage(ProductNameTypePageReqVO pageReqVO) {
        return productNameTypeMapper.selectPage(pageReqVO);
    }

}